package com.xxyf.filter;

import com.fasterxml.jackson.databind.json.JsonMapper;
import com.xxyf.tools.Code;
import com.xxyf.tools.Rest;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author 小小怡飞
 * @Date 2022/12/7 23:26
 * @Version JDK 8
 */
@Component
public class AccessDeniedHandlerImp implements AccessDeniedHandler {
    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException {
        Rest rest =new Rest(1002,null,"权限不足");

        response.setStatus(200);
        response.setContentType("application/json");
        response.setCharacterEncoding("utf-8");
        JsonMapper jsonMapper = new JsonMapper();

        response.getWriter().print(jsonMapper.writeValueAsString(rest));
    }
}
